/*
 * @Author: PanZhangOne
 * @Date: 2017-12-15 10:06:32
 * @Last Modified by: PanZhangOne
 * @Last Modified time: 2017-12-16 11:51:28
 */
const User = require('../models/user');

/**
 * 操作用户积分方法，通过传入不同的操作符来做不同的事
 * @method operateUserIntegral
 * @param {string} id - 用户的_id
 * @return {Function}
 */
const operateUserIntegral = (id) => {
  return function (num, operator) {
    return new Promise((resolve, reject) => {
      User.findById(id)
        .then(user => {
          if (operator === '-') {
            user.integral = user.integral - num;
          } else {
            user.integral = user.integral + num;
          }
          user.save()
            .then((user) => resolve(user))
            .catch(err => reject(err))
        })
        .catch(err => reject(err));
    });
  };
};

/**
 * 设置用户状态
 * @method setUserState
 * @param {string} id - 用户的_id
 * @param {boolean} state - 状态 true, false
 * @returns {Promise}
 */
const setUserState = (id, state) => {
  return new Promise((resolve, reject) => {
    User.find(id)
      .then(user => {
        user.state = state;
        user.save()
          .then(user => resolve(user))
          .catch(err => reject(err));
      })
      .catch(err => reject(err));
  });
};

/**
 * TODO: 分页查询
 * 获取用户列表
 */
const getUserList = () => {
  return new Promise((resolve, reject) => {
    User.find({ state: true }, 'name email signature avatar integral loginIp createAt topicCount replyCount')
      .then(users => resolve(users))
      .catch(err => reject(err));
  });
};

// 获取最新N条会员信息
const getSomeUsers = (num) => {
  return new Promise((resolve, reject) => {
    User.find({ state: true }, 'name email loginIp')
      .sort({ createAt: -1 })
      .limit(num)
      .then(users => resolve(users))
      .catch(err => reject(err));
  });
};

/**
 * 通过id获取用户信息
 * @param {string} id - 用户_id
 */
const getUserById = (id) => {
  return new Promise((resolve, reject) => {
    User.findById(id)
      .then(user => resolve(user))
      .catch(err => reject(err));
  });
};

/**
 * 修改用户信息
 * @param {string} id - 用户的_id
 * @param {object} infos - 要修改的信息
 */
const setUserInfo = (id, infos) => {
  if (!infos && !(typeof infos === 'object')) return Promise.reject('要修该项不能为空');

  return new Promise((resolve, reject) => {
    User.findById(id)
      .then(user => {
        for (let item in infos) {
          user[item] = infos[item];
        }

        user.save()
          .then(user => resolve(user))
          .catch(err => reject(err));
      })
      .catch(err => reject(err));
  });
};

// 获取用户总数
const getUsersCount = () => {
  return new Promise((resolve, reject) => {
    User.count()
      .then(count => resolve(count))
      .catch(err => reject(err));
  });
};

/**
 * 获取某时段的用户
 * 2017-12-12
 * @param {string} type 类型 count数量 null列表
 * @param {string} start 开始时间
 * @param {string} end  结束时间
 */
const getSometimeNewUser = (type, start, end) => {
  switch (type) {
    case 'count':
      return new Promise((resolve, reject) => {
        User.collection.count({
          createAt: {
            '$gte': new Date(start),
            '$lt': new Date(end)
          }
        })
          .then(users => resolve(users))
          .catch(err => reject(err));
      });
    default:
      return new Promise((resolve, reject) => {
        User.collection.find({
          createAt: {
            '$gte': new Date(start),
            '$lt': new Date(end)
          }
        })
          .then(users => resolve(users))
          .catch(err => reject(err));
      });
  }
};

module.exports = {
  operateUserIntegral,
  setUserState,
  getUserList,
  getUserById,
  setUserInfo,
  getUsersCount,
  getSometimeNewUser,
  getSomeUsers
};
